package com.turku.folipass;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;

public class MainActivity extends Activity {
	
	public static AutoCompleteTextView fromEditText;
    public static AutoCompleteTextView toEditText;
	
    List<String> addresslist = new ArrayList<String>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		addresslist = readFromFile();
		
		Button btnNextScreen = (Button) findViewById(R.id.btnNextScreen); 
		
		ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
                android.R.layout.simple_dropdown_item_1line, addresslist);
		ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,
                android.R.layout.simple_dropdown_item_1line, addresslist);
		
		fromEditText = (AutoCompleteTextView) findViewById(R.id.editText1);
        fromEditText.setHint(R.string.maEditFromHint);
        
        toEditText = (AutoCompleteTextView) findViewById(R.id.editText2);
        toEditText.setHint(R.string.maEditToHint);
                
        fromEditText.setAdapter(adapter1);
        toEditText.setAdapter(adapter2);
		
		// Listening to button event
		btnNextScreen.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {
				// Starting a new Intent
				Intent nextScreen = new Intent(getApplicationContext(),
						XMLdata.class);

				// Sending data to another Activity
				nextScreen.putExtra("spos", fromEditText.getText().toString());
				nextScreen.putExtra("epos", toEditText.getText().toString());

				// starting new activity
				startActivity(nextScreen);

			}
		});
	}
private List<String> readFromFile() {
        List<String> stringBuilder = new ArrayList<String>();
        try {
            InputStream inputStream = this.getAssets().open("addresslist.txt");
             
            if ( inputStream != null ) {
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                String receiveString = "";
                while ( (receiveString = bufferedReader.readLine()) != null ) {
                    stringBuilder.add(receiveString);
                }
                inputStream.close();
            }
        }
        catch (FileNotFoundException e) {
            Log.d("Address", "File not found: " + e.toString());
        } catch (IOException e) {
            Log.d("Address", "Can not read file: " + e.toString());
        }
        return stringBuilder;
    }
}
